package com.czy.owner.widget;

import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.ActionMode;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.EditText;

import com.czy.owner.utils.MyLog;
import com.czy.owner.utils.PhoneUtils;
import com.czy.owner.utils.StringUtils;

/**
 * Created by hedepu on 2017-11-17
 */

public class MoneyEditText extends EditText {
    private int INTEGER_COUNT_FOUR = 6;
    private int DECIMAL_COUNT = 2;
    private String inputAfterText;
    private double maxMoney = 0;
    private String showMessage;
    private int cursorPos;
    private OnEtitClickLinear onEtitClickLinear;

    public void setOnEtitClickLinear(OnEtitClickLinear onEtitClickLinear) {
        this.onEtitClickLinear = onEtitClickLinear;
    }

    public void setMaxMoney(double maxMoney) {
        this.maxMoney = maxMoney;
    }

    public void setShowMessage(String showMessage) {
        this.showMessage = showMessage;
    }

    public MoneyEditText(Context context) {
        super(context);
        initEditText();
    }

    public MoneyEditText(Context context, AttributeSet attrs) {
        super(context, attrs);
        initEditText();
    }

    public MoneyEditText(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        initEditText();
    }

    private void initEditText() {
//        setKeyListener(null);
        setCustomSelectionActionModeCallback(new ActionMode.Callback() {

            @Override
            public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
                return false;
            }

            @Override
            public void onDestroyActionMode(ActionMode mode) {

            }

            @Override
            public boolean onCreateActionMode(ActionMode mode, Menu menu) {
                return false;
            }

            @Override
            public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
                return false;
            }
        });
        addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int before, int count) {
                inputAfterText = s.toString();
                cursorPos = getSelectionEnd();

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                MyLog.e("hep", s.toString());
                try {
                    if (start == 0 && s.toString().equals(".") && count == 1) {
                        //输入的第一个字符为"."
                        setText("");
                    } else if (s.toString().length() >= 1 && s.toString().substring(0, 1).equals(".")) {
                        setText("0" + s.toString());
                        setSelection(1);
                    } else if (s.length() >= 1) {
                        //当整数位数输入到达被要求的上限,并且当前在输入字符,而不是减少字符
                        if (!StringUtils.isEmpty(s.toString()) && Double.parseDouble(s.toString()) > maxMoney) {
                            setText(inputAfterText);
//                        setSelection(cursorPos > getText().toString().length() ? getText().toString().length() : cursorPos);
                            setSelection(getText().toString().length());
                            PhoneUtils.ShowToastMessage(getContext(), showMessage);
                        } else if (s.toString().contains(".")) {
                            //当前输入的有"."字符
                            String[] text = s.toString().split("\\.");
                            if (text.length >= 2 && text[text.length - 1].length() > DECIMAL_COUNT) {
                                //小数位数超数
                                String beforeD = text[0];
                                if (text[0].length() > INTEGER_COUNT_FOUR) {
                                    beforeD = text[0].substring(0, INTEGER_COUNT_FOUR);
                                }
                                String afterD = text[text.length - 1];
                                if (text[text.length - 1].length() > DECIMAL_COUNT) {
                                    afterD = text[text.length - 1].substring(0, DECIMAL_COUNT);
                                }
                                if (beforeD.length() == 0) {
                                    beforeD = 0 + "";
                                }
                                String resultText = beforeD + "." + afterD;


                                setText(resultText.trim());
                                setSelection(resultText.length());
                            } else if (text.length >= 1 && text[0].length() > INTEGER_COUNT_FOUR) {
                                String beforeD = text[0];
                                if (text[0].length() > INTEGER_COUNT_FOUR) {
                                    beforeD = text[0].substring(0, INTEGER_COUNT_FOUR);
                                }
                                String resultText = beforeD + "." + (text.length == 1 ? "" : text[text.length - 1]);
                                setText(resultText.trim());
//                            setSelection(cursorPos > resultText.length() ? resultText.length() : cursorPos);
                                setSelection(resultText.length());
                            }
                        } else {
                            if (s.length() > INTEGER_COUNT_FOUR) {
                                setText(s.subSequence(0, INTEGER_COUNT_FOUR));
                                setSelection(INTEGER_COUNT_FOUR);
                            } else {

                            }
                        }

                    }
                } catch (Exception e) {
                    setText(inputAfterText);
//                        setSelection(cursorPos > getText().toString().length() ? getText().toString().length() : cursorPos);
                    setSelection(getText().toString().length());
//                    PhoneUtils.ShowToastMessage(getContext(), showMessage);
                }


//                if (s.toString().contains(".") && s.toString().length() > 1 && !StringUtils.isEmpty(s.toString()) && Double.parseDouble(s.toString()) > maxMoney) {
//                    setText(inputAfterText);
//                    setSelection(cursorPos > getText().toString().length() ? getText().toString().length() : cursorPos);
//                    PhoneUtils.ShowToastMessage(getContext(), showMessage);
//                }
                if (onEtitClickLinear != null) {
                    onEtitClickLinear.editClick(StringUtils.getString(getText().toString(), "0"));
                }

            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });
    }

    public interface OnEtitClickLinear {
        void editClick(String text);
    }
}
